package com.webchat.pgc.messagequeue.consumer.redis;

import com.webchat.common.enums.messagequeue.MessageQueueEnum;
import com.webchat.common.service.messagequeue.consumer.AbstractRedisQueueConsumer;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.dto.queue.MallProductStockUpdateMessageDTO;
import com.webchat.pgc.service.mall.MallProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class MallProductStockUpdateRedisQueue extends AbstractRedisQueueConsumer<MallProductStockUpdateMessageDTO> {


    @Autowired
    private MallProductStockService productStockService;

    @Override
    protected MallProductStockUpdateMessageDTO convert(String s) {

        return JsonUtil.fromJson(s, MallProductStockUpdateMessageDTO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_MALL_PRODUCT_UPDATE_STOCK_MESSAGE;
    }

    @Override
    protected void receive(MallProductStockUpdateMessageDTO data) {
        // TODO 队列消费逻辑
        productStockService.updateSkuStock(data);
    }

    @Override
    protected void error(MallProductStockUpdateMessageDTO data, Exception ex) {

    }

    /**
     * 允许最大回队次数
     *
     * @return
     */
    @Override
    protected int getBackQueueTimes() {
        // TODO 这里再想想
        return Integer.MAX_VALUE;
    }

    /**
     * 消费失败，允许立刻重试一次
     *
     * @return
     */
    @Override
    protected int getRetryTimes() {
        return 1;
    }
}
